﻿using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for CreateSolutionView.xaml
    /// </summary>
    public partial class CreateSolutionView : Window
    {
        private Solution solution;
        public CreateSolutionView()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Register(this);

            BindSolutionType();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            double tmpWidth, tmpHeight;
            Version tmpVer, tmpRTVer;
            if (string.IsNullOrWhiteSpace(tbName.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbName.Name, tbName.Text));
                return;
            }
            if (string.IsNullOrWhiteSpace(tbCaption.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbCaption.Name, tbCaption.Text));
                return;
            }
            if (string.IsNullOrWhiteSpace(tbType.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbType.Name, tbType.Text));
                return;
            }
            if (string.IsNullOrWhiteSpace(tbVersion.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbVersion.Name, tbVersion.Text));
                return;
            }
            else
            {
                if(!Version.TryParse(tbVersion.Text, out tmpVer))
                {
                    CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbVersion.Name, tbVersion.Text));
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(tbRTVersion.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbRTVersion.Name, tbRTVersion.Text));
                return;
            }
            else
            {
                if (!Version.TryParse(tbRTVersion.Text, out tmpRTVer))
                {
                    CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbRTVersion.Name, tbRTVersion.Text));
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(tbWidth.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbWidth.Name, tbWidth.Text));
                return;
            }
            else
            {
                if (!double.TryParse(tbWidth.Text, out tmpWidth))
                {
                    CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbWidth.Name, tbWidth.Text));
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(tbHeight.Text))
            {
                CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbHeight.Name, tbHeight.Text));
                return;
            }
            else
            {
                if (!double.TryParse(tbHeight.Text, out tmpHeight))
                {
                    CustomMessageBox.ShowWarning(string.Format("{0}的{1}不正确", tbHeight.Name, tbHeight.Text));
                    return;
                }
            }

            this.IsEnabled = false;
            solution = new Solution(Guid.NewGuid(), tbName.Text, tbCaption.Text, (SolutionType)(tbType.SelectedItem as FrameworkElement).Tag, tmpWidth, tmpHeight, tmpVer, tmpRTVer);

            BeginCreateSolutionEvent @event = new BeginCreateSolutionEvent(UIContext.Current.Token, tbName.Text, tbCaption.Text, tbType.Text, tbVersion.Text, tbRTVersion.Text);
            NCLEventBus.Publish(@event);
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void BindSolutionType()
        {
            foreach (Designtime.Metadata.SolutionType solutionType in Designtime.Metadata.SolutionType.EnumerateTypes())
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = solutionType.Text;
                cbi.Tag = solutionType;
                tbType.Items.Add(cbi);
            }
            tbType.SelectedIndex = 0;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NCLEventBus.Unregister(this);
        }
    }
}
